<?php
namespace App\Controllers;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use App\Models\UserModel;
use stdClass;
class UserController extends BaseController {
	public function UserList($request, $response, $args) {
		include APP_PATH . "Views/User/data.html";
	}

	public function UserImg($request, $response, $args) {
		$srcName = $_FILES['headImg']['name'];
		function get_basename($filename) {
			return preg_replace('/^.+[\\\\\\/]/', '', $filename);
		}

		copy($_FILES['headImg']['tmp_name'], "static/img/" . iconv("UTF-8", "gb2312", get_basename($srcName)));
		//		echo $srcName;
		$src = "/static/img/$srcName";
		$username = $_SESSION["UserName"];
		$data = array('Image' => $srcName);
		$user = new UserModel();
		$ret = $user -> Img($username, $data);
		if ($ret != 0) {
			return $response -> withRedirect($this -> container -> router -> pathFor('Self'));
		}

	}

	public function getData() {
		$user = new UserModel();
		$user -> getDate();
	}

	public function welcome($request, $response, $args) {
		$username = $_SESSION["UserName"];
		if ($username != "") {
			include APP_PATH . "Views/User/home.html";
		} else {
			include APP_PATH . "Views/User/login.html";

		}

	}

	public function UserShow($request, $response, $args) {
		include APP_PATH . "Views/User/self.html";
	}

	public function getSelf() {
		$username = $_SESSION["UserName"];
		$user = new UserModel();
		$info = $user -> getSelf($username);
		echo $info;
		//		echo $username;

	}

	public function Error($request, $response, $args) {
		include APP_PATH . 'Views/405.html';
	}

	public function postLogin($request, $response, $args) {

		$username = $request -> getParam('username');
		$password = $request -> getParam('psw');
		$user = new UserModel();
		$info = $user -> getUserInfo($username, $password);
		
		if ($user -> getUserInfo($username, $password)) {
			$_SESSION["UserName"] = $username;
			$data = $info;
		} else {
			$data = array('message' => '用户名或密码不正确', 'code' => 101);
		}
		echo json_encode($data);
	}

	public function Login($request, $response, $args) {
		include APP_PATH . 'Views/User/login.html';
	}

	public function Logout($request, $response, $args) {
		unset($_SESSION["UserName"]);
		include APP_PATH . 'Views/User/login.html';
	}

	public function getAdd($request, $response, $args) {
		include APP_PATH . 'Views/User/useradd.html';
	}

	public function getUpdate($request, $response, $args) {
		include APP_PATH . 'Views/User/userupdata.html';
	}

	public function UserAdd($request, $response, $args) {
		$username = $request -> getParam('username');
		$nickname = $request -> getParam('nickname');
		$pas = $request -> getParam('pas');
		$email = $request -> getParam('email');
		$sex = $request -> getParam('sex');
		$age = $request -> getParam('age');
		$phone = $request -> getParam('phone');
		$address = $request -> getParam('address');
		$user = new UserModel();
		$data = array('UserName' => $username, 'PassWord' => $pas, 'Email' => $email, 'NickName' => $nickname, 'sex' => $sex, 'Phone' => $phone, 'age' => $age, 'Address' => $address);
		//			if($user->userAdd($username,$email,$phone))
		$data1 = $user -> userAdd($username, $email, $phone);
		$msg = json_decode($data1);
		if ($msg -> code == 0) {
			if ($user -> add($data)) {
				$roll = array('message' => '添加成功', 'code' => 2);
				$back = $roll;
			}
		} else {
			$back = $msg;
		}
		echo json_encode($back);

	}

	public function UserUpdate($request, $response, $args) {
		$username = $request -> getParam('username');
		$nickname = $request -> getParam('nickname');
		$email = $request -> getParam('email');
		$sex = $request -> getParam('sex');
		$age = $request -> getParam('age');
		$phone = $request -> getParam('phone');
		$address = $request -> getParam('address');
		$user = new UserModel();
		$data1 = $user -> userupdata($username, $email, $nickname, $sex, $phone, $age, $address);
		$msg = json_decode($data1);
		echo json_encode($msg);

	}

	public function UserDelete($request, $response, $args) {
		$id = $request -> getParam('id');
		$user = new UserModel();
		if ($user -> delete($id)) {
			$data = array('message' => '删除成功', 'code' => 0);
		} else {
			$data = array('message' => '删除失败', 'code' => 101);
		}
		echo json_encode($data);
	}

	public function UserSelect($request, $response, $args) {
		echo "UserSelect";
	}

	public function getPsw($request, $response, $args) {
		include APP_PATH . 'Views/User/uppassword.html';
	}

	public function PswUpdate($request, $response, $args) {
		$username = $request -> getParam('username');
		$password = $request -> getParam('psw');
		$password2 = $request -> getParam('psw2');
		$user = new UserModel();
		$msg = $user -> pswUpdate($username, $password, $password2);
		echo json_encode($msg);
	}

	public function selectPsw($request, $response, $args) {
		include APP_PATH . 'Views/User/selpassword.html';
	}

	public function PswSelect($request, $response, $args) {
		$username = $request -> getParam('username');
		$email = $request -> getParam('email');
		$phone = $request -> getParam('phone');
		$user = new UserModel();
		$msg = $user -> pswSelect($username, $email, $phone);
		echo json_encode($msg);

	}

}
?>